<?php

namespace app\v1\controller;

use think\Controller;
use think\Request;

/**
 * 客方信息类
 */
class Rooms extends Base
{
    public function list_read($page = 0)
    {
        if (!input('?start_time') || !input('?end_time') ||
        empty(input('start_time')) || empty(input('end_time')) ) {
            return Result(0, '请选择时间');
        }
        $option = [];

        $optionKey = ['region_id', 'specifications_id', 'start_time', 'end_time'];

        foreach ($optionKey as $value) {
            if (input("?$value")) {
                $option[$value] = input($value);
            }
        }

        return model('Rooms')->GetList($page, $option);
    }

    public function read($id = 0)
    {
        if ($id === 0) {
            return Result(0, '请选择想要浏览的民宿');
        }
        return model('Rooms')->GetInfo($id);
    }

    public function reservation($id = 0)
    {
        $criteria = false;
        if (IsLogin() === false) {
            return Result(0, '请登陆后在预定');
        }

        if (empty($id)) {
            return Result(0, '请选择正确的房间');
        }
        $start_time = input('?start_time') ? input('start_time') : $criteria = true;
        $end_time = input('?end_time') ? input('end_time') : $criteria = true;

        $time = date('Y-m-d');
        if ($start_time < $time  || $end_time <= $time || $start_time < $end_time) {
            $criteria = true;
        }
        if ($criteria) {
            return Result(0, '请选择正确的时间');
        }
        $mobile = 0;
        if (input('?mobile')) {
            $mobile = input('mobile');
        }
        return model('Rooms')->ReservationRooms($id, $GLOBALS['user_id'], $start_time, $end_time, $mobile);
    }

    public function empty_rooms($page = 0)
    {
        if (!input('?start_time') || !input('?end_time') ||
        empty(input('start_time')) || empty(input('end_time')) ) {
            $option['start_time'] = date('Y-m-d');
            $option['end_time'] = date('Y-m-d', strtotime('+1 day'));
        }

        $optionKey = ['start_time', 'end_time', 'specifications_id', 'region_id'];
        $option = [];
        foreach ($optionKey as $value) {
            if (input("?{$value}") && !empty(input($value))) {
                $option[$value] = input($value);
            }
        }

        return model('Rooms')->GetEmptyRoomsList($page, $option);
    }
}
